﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Windows.Forms;

namespace ConvertTextToPDF
{
    public partial class FrmMain : Form
    {
        private Biz biz = Biz.Instance;

        private string extName = ".txt";

        private DataTable dt = new DataTable();

        public FrmMain()
        {
            InitializeComponent();

            InitControl();

        }

        private void InitControl()
        {
            biz.OnFoundDirReport = OnFoundDirReport;

            biz.OnFoundReport = OnFoundReport;

            biz.OnReportProcess = OnReportProcess;
        }

        private void InitDataTable()
        {
            dt.Columns.AddRange(new DataColumn[] {
                new DataColumn("fileName",typeof(string)),
                new DataColumn("filePath",typeof(string)),
                new DataColumn("fileExtName",typeof(string)),
                new DataColumn("fileSize",typeof(int)),
                new DataColumn("fileCreateDate",typeof(DateTime))
            });
        }

        private void BindGridViewData()
        {
            dgvList.DataSource = dt;
        }

        private bool OnReportProcess(int i)
        {
            bwFindFile.ReportProgress(i);

            return true;
        }
        private bool OnFoundDirReport(DirectoryInfo dirInfo)
        {
            txtCurPath.Text = string.Format("正在查找当前目录中的PDF:{0}", dirInfo.Name);

            return true;
        }

        private bool OnFoundReport(FileInfo fileInfo)
        {
            DataRow dr = dt.NewRow();

            dr["fileName"] = fileInfo.Name;

            dr["filePath"] = fileInfo.FullName;

            dr["fileExtName"] = fileInfo.Extension.Replace(".",string.Empty);

            dr["fileSize"] = fileInfo.Length;

            dr["fileCreateDate"] = fileInfo.CreationTime ;

            dt.Rows.Add(dr);

            BindGridViewData();

            return true;
        }

        private void TSBExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void TSBtnFind_Click(object sender, EventArgs e)
        {
            if (fBDTextExplorer.ShowDialog().Equals(DialogResult.OK))
            {

            }
        }

        private void bwFindFile_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            Events.OnFindFileList onFindFileList = null;

            biz.FindAllTextFileList(fBDTextExplorer.SelectedPath, extName, (onFindFileList = OnFindFileList));
        }

        private void bwFindFile_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
        {
            TSProcessForAll.Value = e.ProgressPercentage;
        }

        private void bwFindFile_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {

        }

        private void OnFindFileList(List<FileInfo> list)
        {

        }
    }
}
